package holdobject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

public class ReversibleArrayList<T> extends ArrayList<T> {//Array数组
    //List列表
    public ReversibleArrayList(Collection<T> c){
        super(c);//对c的处理委托给超类的构造函数，且自动插入子类中
    }

    public Iterable<T> reversed(){
        return new Iterable<T>() {
            @Override
            public Iterator<T> iterator() {
                return new Iterator<T>() {
                    int current=size()-1;
                    @Override
                    public boolean hasNext() {
                        return current>-1;
                    }

                    @Override
                    public T next() {
                        return get(current--);
                    }
                };
            }
        };
    }
}
class AdapterMethodIdiom{
    public static void main(String[] args){
        ReversibleArrayList<String> re=new ReversibleArrayList<String>(Arrays.asList("To be or not to be".split(" ")));
        for (String s:re){
            System.out.print(s+" ");
        }
        System.out.println();
        for (String s:re.reversed()){
            System.out.print(s+" ");
        }
    }
}